package com.dengqi.mymodule.activity.camera;

import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.dengqi.mymodule.BaseActivity;
import com.dengqi.mymodule.R;
import com.dengqi.mymodule.tools.BitmapUtils;
import com.dengqi.mymodule.tools.UriUtils;

import java.io.File;


public class CameraOutputActivity extends BaseActivity {

    ImageView imageView;
    static final int REQUEST_CODE_CAMERA = 1;
    static final int REQUEST_CODE_ALBUM = 2;
    static final int REQUEST_CODE_CROP = 3;
    //目标file，最后输出的file
    File targetFile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera_output);
        imageView = findViewById(R.id.imageView);
        ///storage/emulated/0/Android/data/com.dengqi.mymodule/files/camera/target.jpg
        //存在外部私有路径，软件卸载后删除
        targetFile = new File(getExternalFilesDir("camera"), "target.jpg");
        Log.e("---", targetFile.getAbsolutePath());
        Log.e("---", "是否存在：" + targetFile.exists());
    }


    public void onBtn1(View view) {
        startCamare();
    }

    public void onBtn2(View view) {
        //获取原图file
//        File file = new File(Environment.getExternalStorageDirectory(),"yummy.png");
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "pic1.jpg");
        if (file.exists()) {
            startCrop(file);
        } else {
            Toast.makeText(this, "文件不存在", Toast.LENGTH_SHORT).show();
        }
    }

    public void onBtn31(View view) {
    }

    public void onBtn32(View view) {
//        startAlbum(2);
    }

    public void onBtn33(View view) {
//        startAlbum(3);
    }

    private void startCamare() {
        //目标file转成uri
        Uri targetUri = UriUtils.file2Uri(this, targetFile);
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        //将拍照结果保存至Uri中
        //7.0uri申请写入权限，只能写在MediaStore.EXTRA_OUTPUT前面
        boolean isSuccess = UriUtils.applyUri(this, intent, targetUri);
        if (isSuccess) {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, targetUri);
            startActivityForResult(intent, REQUEST_CODE_CAMERA);
        }
    }

    private void startCrop(File sourceFile) {
        //file->uri
        Uri sourceUri = UriUtils.file2Uri(this, sourceFile);
        Uri targetUri = UriUtils.file2Uri(this, targetFile);
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(sourceUri, "image/*");
        intent.putExtra("return-data", false);
        //7.0uri申请写入权限，只能写在MediaStore.EXTRA_OUTPUT前面
        boolean isSuccess = UriUtils.applyUri(this, intent, targetUri);
        if (isSuccess) {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, targetUri);
            intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
            startActivityForResult(intent, REQUEST_CODE_CROP);
        }
    }


    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_CAMERA && resultCode == RESULT_OK) {
            //由于指定了目标uri，存储在目标uri，intent.putExtra(MediaStore.EXTRA_OUTPUT, uri)
            //目标file转成bitmap，获取1024x1024像素缩略图
            Bitmap bitmap = BitmapUtils.file2Bitmap(targetFile, 1024, 1024);
            imageView.setImageBitmap(bitmap);
            //存下来只有100KB左右
//                ImageFileUtils.writeBitmap2File(bitmap, "camera.jpg");
            //删除目标file
            if (targetFile != null && targetFile.exists()) {
//                targetFile.delete();
            }
        } else if (requestCode == REQUEST_CODE_CROP && resultCode == RESULT_OK) {
            //由于指定了目标uri，存储在目标uri，intent.putExtra("return-data", false);
            //目标file转成bitmap，获取1024x1024像素缩略图
            Bitmap bitmap = BitmapUtils.file2Bitmap(targetFile, 1024, 1024); //BitmapUtils.uri2Bitmap(targetUri);
            imageView.setImageBitmap(bitmap);
            //存下来只有100KB左右
//                ImageFileUtils.writeBitmap2File(bitmap, "crop.jpg");
            //删除目标file
            if (targetFile != null && targetFile.exists()) {
                targetFile.delete();
            }
        }
    }

}
